//--------------------------------------------------------------------------
// 
//  Copyright (c) Christian Liensberger.  All rights reserved. 
// 
//  Licensed under GPL v2. See official license for details.
// 
//--------------------------------------------------------------------------

using System;
using System.Windows.Forms;

namespace Chili.Imagizer
{
	public partial class MainForm : Form
	{
		private ImagesCollection _images;

		public MainForm()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Invoked each time a drop has occured.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void panel1_DragDrop(object sender, DragEventArgs e)
		{
			string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
			ImagesLoader loader = new ImagesLoader();
			loader.ImageFound += new EventHandler<ImageLoadedEventArgs>(loader_ImageFound);
			_images = loader.GetImageFiles(files, true);

			// Signalize that all images have been found.
			readingImagesLabel.Text = "All images loaded! Total: " + _images.Count + ". Please select your options now:";
			okButton.Enabled = true;
			outputDirectoryTextBox.Text = _images.RootDirectory.FullName + "\\" + DateTime.Now.ToShortDateString() + "-Output";
		}

		/// <summary>
		/// Invoked each time a file has been found by the loader.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void loader_ImageFound(object sender, ImageLoadedEventArgs e)
		{
			readingImagesLabel.Text = "Loading: " + e.File.FullName;
			Application.DoEvents();
		}

		/// <summary>
		/// Invoked each time a drag enters the control.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void panel1_DragEnter(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
				e.Effect = DragDropEffects.All;
		}

		/// <summary>
		/// Invoked each time the cancel button is pressed.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void cancelButton_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		/// <summary>
		/// Invoked each time the output directory button is clicked.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void outputDirectoryButton_Click(object sender, EventArgs e)
		{
			folderBrowserDialog1.SelectedPath = outputDirectoryTextBox.Text;
			if (folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
			{
				outputDirectoryTextBox.Text = folderBrowserDialog1.SelectedPath;
			}
		}

		/// <summary>
		/// Invoked when the process button is clicked.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void okButton_Click(object sender, EventArgs e)
		{
			try
			{
				this.Cursor = Cursors.WaitCursor;

				ImagesProcessor p = new ImagesProcessor();
				p.ImageProcessing += new EventHandler<ImageProcessingEventArgs>(p_ImageProcessing);
				p.ProcessImages(outputDirectoryTextBox.Text, (float)numericUpDown1.Value / 100.0f, _images);

				processingLabel.Text = "Finished processing. All files have been written to the output directory.";
				progressBar1.Value = progressBar1.Maximum;
			}
			finally
			{
				this.Cursor = Cursors.Default;
			}
		}

		/// <summary>
		/// Invoked each time a image is been processed.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void p_ImageProcessing(object sender, ImageProcessingEventArgs e)
		{
			processingLabel.Text = string.Format("Processing image '{0}' ({1} of {2})", e.File.Name, e.Index + 1, e.Count);
			progressBar1.Maximum = e.Count;
			progressBar1.Value = e.Index;

			Application.DoEvents();
		}
	}
}